package com.wl.eb.controller;

import com.wf.captcha.utils.CaptchaUtil;
import com.wl.eb.module.EbUser;
import com.wl.eb.service.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/login")
public class LoginController {
    @GetMapping("/on")
    public String loginPage(){
        return "login";
    }

    @GetMapping("/reg")
    public String loginReg(){ return "register"; }

    @Resource
    private LoginService loginService;

    @PostMapping
    public String login(EbUser ebUser, String verycode,
                        HttpServletRequest request){
        System.out.println("=====LoginController的login,ebuser:"+ebUser);
        //yan
        //0.校验验证码
        if(!verycode.equals(request.getSession().getAttribute("captcha"))){
            throw new RuntimeException("小青是美女！");
        }

        //
        EbUser loginUser =loginService.login(ebUser);
        //
        request.getSession().setAttribute("lgnUser",loginUser);
        //
        return "redirect:index/index";
        }

        @PostMapping("register")

    public String register(EbUser ebUser){
            System.out.println("=====LoginController的register,ebuser:"+ebUser);
            //
            boolean result=loginService.register(ebUser);
            //
            //
            return "redirect:on";
        }


    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }



}
